var preload;
var manifest;
var oLoadBitmap;
// đối tượng chứa câu hỏi và câu trả lời.
function QandA(){
	this.pictures;
	this.answers;
	this.rawQuestion;
	this.sumQuestion;


	this.URL = "http://data2.yome.vn";

	
	//Anh da duoc load ve hien tai + thong tin anh hien tai
	this.currentBitmap	= null;
	this.currentAnswer	= null;
	this.currentLevel	= null;
	this.currentUrlImage= null;
	//Anh da duoc load ve tiep theo + thong tin anh tiep theo
	this.nextBitmap	= null;
	this.nextAnswer	= null;
	this.nextLevel	= null;
	this.nextUrlImage= null;


	//So luong cau hoi tai lan tai hien tai
	this.lenghQuestionAtCurrentLoad = null;


	//load  anh thu i
	this.iLoadImage = null;

	//Bien danh dau dang trong qua trinh tai anh
	// value = 0: chua tai || tai xong
	// value = 1: dang tai
	//this.flagProcessLoadImage = 0;

	this.getQuestion = function(){
		if (window.isGetDataUser) {
			window.isReady = false;
			var level = savescore.getUserLevel();
			var xmlHttp;
			if(window.XMLHttpRequest){
				xmlHttp = new XMLHttpRequest();	
			}else{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");		
			}

			$.ajax({
					type: 'post',
					data: {
						level: level,
					},
					url: 'php/getQuestion.php',
					success: function(data) {
						if(data != '0'){
							window.qanda.processQA(data);
						} else {
							if(confirm("Không thể kết nối tới cơ sở dữ liệu. Refresh trang và tiếp tục chơi"))
								window.setTimeout(function(){location.reload();},0);
						}
					}
			});
		} else {
			window.setTimeout(window.qanda.getQuestion, 200);
		}
	}


	this.processQA = function(string){
		var tempArr = string.split('|');
		var length = tempArr.length;
		this.lenghQuestionAtCurrentLoad = length;
		this.initialQA(length);
		for(i = 0; i < length ; i++){
			var x = tempArr[i].split('-');
			this.pictures[i] 	= x[1];
			this.answers[i] 	= x[2];
			this.levels[i] 		= x[0];
		}

		//auto load first image
		this.iLoadImage = 0;
		this.loadImageFromUrl();
	}


	//Khoi tao mang chua anh
	//@input: length: do dai mang
	this.initialQA = function(length) {
		this.pictures	= null;
		this.answers  	= null;
		this.levels   	= null;
		this.pictures 	= new Array(length);
		this.answers 	= new Array(length);
		this.levels 	= new Array(length);
	}


	//Auto Load Image
	this.loadImageFromUrl = function() {
		//this.flagProcessLoadImage = 1; //Dang trong qua trinh tai
		var src = this.URL + this.pictures[this.iLoadImage];
		setupManifest(src);
		startPreload();
	}

	this.completeLoadImage = function() {
		//this.flagProcessLoadImage = 0; //Da tai xong
		//Load lan dau la tai vao currentBitmap , con nguoc lai la nextBitmao
		if (this.currentBitmap == null) {
			this.currentBitmap	= oLoadBitmap;
			this.currentAnswer 	= this.answers[this.iLoadImage].toUpperCase();
			this.currentLevel	= this.levels[this.iLoadImage];
			this.currentLevel	= this.levels[this.iLoadImage];
		} else {
			this.nextBitmap		= oLoadBitmap;
			this.nextAnswer 	= this.answers[this.iLoadImage];
			this.nextLevel		= this.levels[this.iLoadImage];	
			this.nextLevel		= this.levels[this.iLoadImage];				
		}
	}


	//Tai anh tiep theo
	this.loadNextImage 	 = function() {
		this.iLoadImage++;
		if (this.iLoadImage >= this.lenghQuestionAtCurrentLoad) {
			this.getQuestion(savescore.level);
		} else
			this.loadImageFromUrl();
	}

	this.nextToCurrent	 = function() {
		this.currentAnswer = this.nextAnswer;
		this.currentBitmap = this.nextBitmap;
		this.currentLevel  = this.nextLevel;
	}


	this.getAnswer	= function() {
		return this.currentAnswer;
	}

	this.getBitmap  = function() {
		return this.currentBitmap;
	}

	this.getLevel 	= function() {
		return this.currentLevel;
	}
}




/*************FUNCTION PRELOADJS IMAGE****************/

function setupManifest(src) {
	manifest = {src:src, id: "loadImage"};
}

function startPreload() {
	preload = new createjs.LoadQueue(true);
	preload.setUseXHR(false);
/*		this.preload.addEventListener("fileload", this.handleFileLoad);
	this.preload.addEventListener("complete", this.loadComplete);*/
	preload.on("fileload", handleFileLoad, this);
	preload.on("complete", handleComplete, this);
	preload.loadFile(this.manifest);
}


function handleFileLoad(event) {
    console.log("A file has loaded of type: " + event.item.type);
    if(event.item.id == "loadImage"){
        console.log("image is loaded");
        var picture = preload.getResult("loadImage");
        console.log(picture);
        oLoadBitmap = new createjs.Bitmap(picture);
		qanda.completeLoadImage();
    }
}


function handleComplete(event) {
	window.isReady = true;
	console.log("Finished Loading Assets");
}


